package cn.edu.zafu.bigdata.fui;

import java.sql.SQLException;
import java.util.Scanner;

public class StudentManagementSys{
    public static void main(String[] args) {
        DBHelper.createTable();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("学生信息管理系统");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    try {
                        addStudent(scanner);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    try {
                        searchStudent(scanner);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    break;
                case 3:
                    try {
                        updateStudent(scanner);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    break;
                case 4:
                    try {
                        deleteStudent(scanner);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    break;
                case 5:
                    System.out.println("退出程序。");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的输入，请重新输入。");
                    break;
            }
        }
    }

    private static void addStudent(Scanner scanner) throws SQLException {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        while (DBHelper.getStudent(id) != null) {
            System.out.println("学号重复，请重新输入学号: ");
            id = scanner.nextLine();
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        Student student = new Student(id, name, phone);
        DBHelper.addStudent(student);
    }

    private static void searchStudent(Scanner scanner) throws SQLException {
        System.out.print("请输入学号检索学生信息: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            Student student = DBHelper.getStudent(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生");
            }
            System.out.print("请输入学号检索学生信息(输入QUIT退出): ");
            id = scanner.nextLine();
        }
    }

    private static void updateStudent(Scanner scanner) throws SQLException {
        System.out.print("请输入学号更新学生信息: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            Student student = DBHelper.getStudent(id);
            if (student != null) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                DBHelper.updateStudent(student);
            } else {
                System.out.println("未找到该学生");
            }
            System.out.print("请输入学号更新学生信息(输入QUIT退出): ");
            id = scanner.nextLine();
        }
    }

    private static void deleteStudent(Scanner scanner) throws SQLException {
        System.out.print("请输入学号删除学生信息: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            DBHelper.deleteStudent(id);
            System.out.print("请输入学号删除学生信息(输入QUIT退出): ");
            id = scanner.nextLine();
        }
    }
}
